package com.nineclock.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.attendance.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.mapper.SysCompanyUserMapper;
import com.nineclock.system.mapper.SysDepartmentMapper;
import com.nineclock.system.pojo.SysCompanyUser;
import com.nineclock.system.pojo.SysDepartment;
import com.nineclock.system.service.SysDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SysDepartmentServiceImpl implements SysDepartmentService {
    @Autowired
    SysDepartmentMapper sysDepartmentMapper;
    @Autowired
    SysCompanyUserMapper sysCompanyUserMapper;

    @Override
    public List<SysDepartmentDTO> queryDepartmentList() {
        List<SysDepartmentDTO> sysDepartmentDTOList = this.queryChildDepartmentList(0L);
        return sysDepartmentDTOList;
    }

    @Override
    public List<Long> queryDepartmentIds() {
        Long companyId = CurrentUserHolder.get().getCompanyId();

        LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDepartment::getCompanyId,companyId);
        wrapper.eq(SysDepartment::getParentId,0);

        //这里获取到的是顶级部门的信息，但是我们只要id就可以
        List<SysDepartment> sysDepartments = sysDepartmentMapper.selectList(wrapper);
        //多练lambda表达式
        List<Long> ids = sysDepartments.stream()
                .map(department -> {
                    return department.getId();
                })
                .collect(Collectors.toList());

        return ids;
    }

    @Override
    public List<DepartmentOrUserSimpleDTO> querySimpleDepartment(Long departmentId, Integer includeMember) {
        if(departmentId == null || includeMember == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        List<DepartmentOrUserSimpleDTO> list = new ArrayList<>();
        //查询该部门下的子部门
        LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDepartment::getCompanyId,CurrentUserHolder.get().getCompanyId());
        wrapper.eq(SysDepartment::getParentId,departmentId);
        List<SysDepartment> sysDepartments = sysDepartmentMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(sysDepartments)){
            for (SysDepartment sysDepartment : sysDepartments) {
                DepartmentOrUserSimpleDTO departmentOrUserSimpleDTO = new DepartmentOrUserSimpleDTO();
                departmentOrUserSimpleDTO.setType(1);
                departmentOrUserSimpleDTO.setId(sysDepartment.getId());
                departmentOrUserSimpleDTO.setName(sysDepartment.getName());
                list.add(departmentOrUserSimpleDTO);
            }
        }
        if (includeMember == 0){
            LambdaQueryWrapper<SysCompanyUser> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(SysCompanyUser::getCompanyId, CurrentUserHolder.get().getCompanyId());
            userLambdaQueryWrapper.eq(SysCompanyUser::getDepartmentId, departmentId);

            List<SysCompanyUser> sysCompanyUsers = sysCompanyUserMapper.selectList(userLambdaQueryWrapper);
            if(CollectionUtil.isNotEmpty(sysCompanyUsers)){
                for (SysCompanyUser companyUser : sysCompanyUsers) {
                    DepartmentOrUserSimpleDTO userSimpleDTO = new DepartmentOrUserSimpleDTO();
                    userSimpleDTO.setType(2);
                    userSimpleDTO.setId(companyUser.getId());
                    userSimpleDTO.setName(companyUser.getUserName());
                    list.add(userSimpleDTO);
                }
            }
        }
        return list;
    }

    @Override
    public List<Long> queryDepartmentsByUserId(Long companyUserId) {
        List<Long> ids = new ArrayList<>();

        //查询该员工所在部门id
        LambdaQueryWrapper<SysCompanyUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysCompanyUser::getCompanyId, CurrentUserHolder.get().getCompanyId());
        wrapper.eq(SysCompanyUser::getId, companyUserId);
        SysCompanyUser companyUser = sysCompanyUserMapper.selectOne(wrapper);
        if(companyUser == null){
            throw new NcException(ResponseEnum.USER_NOT_FOUND);
        }
        //获取员工所在的部门id
        Long departmentId = companyUser.getDepartmentId();
        ids.add(departmentId);
        while (true){
            //查询该部门的上级部门
            LambdaQueryWrapper<SysDepartment> departmentQw = new LambdaQueryWrapper<>();
            departmentQw.eq(SysDepartment::getCompanyId, CurrentUserHolder.get().getCompanyId());
            departmentQw.eq(SysDepartment::getId, departmentId);
            SysDepartment department = sysDepartmentMapper.selectOne(departmentQw);
            if(department != null){
                //获取上级id
                Long parentId = department.getParentId();
                if(parentId == 0){
                    break;
                }
                ids.add(parentId);
                departmentId = parentId;
            }
        }

        return ids;
    }

    public List<SysDepartmentDTO> queryChildDepartmentList(Long parentId){
        //获取当前企业id
        Long companyId = CurrentUserHolder.get().getCompanyId();
        LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDepartment::getCompanyId,companyId);
        wrapper.eq(SysDepartment::getParentId,parentId);
        List<SysDepartment> sysDepartments = sysDepartmentMapper.selectList(wrapper);
        if (sysDepartments  != null){
            List<SysDepartmentDTO> sysDepartmentDTOList = BeanHelper.copyWithCollection(sysDepartments, SysDepartmentDTO.class);
            for (SysDepartmentDTO sysDepartmentDTO : sysDepartmentDTOList) {
                List<SysDepartmentDTO> childDepartmentList = this.queryChildDepartmentList(sysDepartmentDTO.getId());
                sysDepartmentDTO.setChildren(childDepartmentList);
            }
            return sysDepartmentDTOList;
        }
        return new ArrayList<>();
    }
}


